package com.cms.recursoshumanos.ui.catalogo.solicitud_compra;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.ArticuloCarritoListButtonField;
import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.component.base.TwoColumnField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetScrollManager;
import com.cms.recursoshumanos.container.SearchBarManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.ListaSolicitudCompraHandler;
import com.cms.recursoshumanos.object.SolicitudCompra;
import com.cms.recursoshumanos.object.list.ListaArticuloCarrito;


import com.cms.recursoshumanos.ui.RecursosHumanosScreen;
import com.cms.recursoshumanos.utilities.Tool;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.GridFieldManager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;



/**
 * @author Francisco Trompiz
 *
 * Corporate Mobile Solutions. 2013
 */

public class DetailSolicitudCompraScreen extends RecursosHumanosScreen implements FieldChangeListener 
{

	int cantidadWidth = getFont().getAdvance("00000");
	int precioWidth = getFont().getAdvance("0000000.00");
	int nombreWidth = ((Display.getWidth()*71)/100)- cantidadWidth - precioWidth;
	XYEdges margin = new XYEdges(2, 2, 2, 2);
	int [] intWidth = {nombreWidth+2,cantidadWidth,precioWidth,0};

	
	SearchBarManager sbm;
	ListStyleButtonSetScrollManager buttonSet;
	VerticalFieldManager vfm;
	HorizontalFieldManager hfm;
	
	TwoColumnFieldManager tcfm_solicitud, tcfm_lista_articulo;
	GridFieldManager gfm;
	LabelField lblMontoValue,
				lblMontoText,
				lblSolicitudIdValue,
				lblSolicitudIdText,
				lblFechaSolicitudValue,
				lblFechaSolicitudText,
				lblVacio;
	private CustomButtonColored boton_cancelar_solicitud;
	Border elBorder_Inicio = BorderFactory.createSimpleBorder(new  XYEdges(1, 1, 0, 1));
	Border elBorder_Final = BorderFactory.createSimpleBorder(new  XYEdges(0, 1, 0, 1));
	Border elBorder_Medio = BorderFactory.createSimpleBorder(new  XYEdges(0, 1, 0, 1));

	private static DetailSolicitudCompraScreen instance = null;
	
	private SolicitudCompra sol_compra;
	
	public static DetailSolicitudCompraScreen getInstance(SolicitudCompra sol_compra)
	{
		if(instance == null)
    	{
    		instance = new DetailSolicitudCompraScreen(sol_compra);
    	}
		else
		{
			instance.sol_compra = sol_compra;
			instance.reset();
		}
		
    	return instance;
    }
	
	
	
    private ForegroundManager _foreground;
    
	public DetailSolicitudCompraScreen(SolicitudCompra sol_compra) 
	{
		super( NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
	  	
		this.sol_compra = sol_compra;	
		
		setComponents();
	}
	public void cancelar_solicitud_compra()
	{
		ListaSolicitudCompraHandler handler = new ListaSolicitudCompraHandler();
		handler.cancelarSolicitudCompra(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
											UserInfoCache.getInstance().getUsuario().getPassword(),sol_compra);
	}
	protected void makeMenu(Menu menu, int instance) 
	{
		MenuItem procesarButton = new MenuItem( "Cancelar Solicitud de Compra", 0, 0) {
			public void run() 
			{
				cancelar_solicitud_compra();
			}
		};
		
		// Refresh...
		MenuItem refreshButton = new MenuItem("Actualizar", 0, 0) {
			public void run() 
			{
				reset();
			}
		};
		menu.add(procesarButton);
		menu.add(refreshButton);
		super.makeMenu(menu, instance);
	}
	
	public void reset() 
	{
		
		/*vfm.deleteAll();
		buttonSet.deleteAll();
		pintarMonto();
		//loading = false;
		try 
		{
			if(lista_articulo.cantElementListaArticulo() > 0) 
			{
				pintarListaArticuloCarrito();
			}
			else
			{
				pintarVacio();
			}
		} 
		catch (Exception e) 
		{
			ShowDialogAlert alert = new ShowDialogAlert("Exception in delete fields...");
			alert.run();
		}*/
		//pintarComponents();

	}
	
	public void setComponents() 
	{
		
		setTitle("Detalle de la Solicitud");
		vfm = new VerticalFieldManager( Field.FIELD_HCENTER | VerticalFieldManager.NON_FOCUSABLE );
		_foreground = new ForegroundManager(NO_VERTICAL_SCROLL);
		_foreground.setPadding( 6, 6, 6, 6 );
		vfm.setPadding(10, 0, 0, 0);	
        
        tcfm_solicitud = new TwoColumnFieldManager( Display.getWidth() / 3, USE_ALL_WIDTH );
        tcfm_solicitud.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), 
        		Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        tcfm_solicitud.setPadding( 1, 0, 1, 0 );
        
		buttonSet = new ListStyleButtonSetScrollManager();
		buttonSet.setScreen(this);

		boton_cancelar_solicitud = new CustomButtonColored("Cancelar Solicitud Compra", ButtonField.CONSUME_CLICK | USE_ALL_WIDTH, Constant.COLOR_BUTTON_FOCUS);
		
		boton_cancelar_solicitud.setChangeListener(new FieldChangeListener() 
        {
				public void fieldChanged(Field field, int context)
		    	{ 
		    		ListaSolicitudCompraHandler handler = new ListaSolicitudCompraHandler();
		    		handler.cancelarSolicitudCompra(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
		    										UserInfoCache.getInstance().getUsuario().getPassword(), 
		    										sol_compra);
		   		} 
		});
		
		pintarPantallaSolicitud();
		pintarListaArticuloCarrito();
		
		
		Field separator = new SeparatorField();
		separator.setMargin( 0 , 0 , 0 , 0 );
		separator.setPadding( 0 , 0 , 0 , 0 );

		_foreground.add(tcfm_solicitud);
		//_foreground.add(boton_cancelar_solicitud);
		_foreground.add(separator);
		_foreground.add(buttonSet);
		add( _foreground );
		
	}
	
	public void pintarPantallaSolicitud()
	{
		tcfm_solicitud.deleteAll();
        
        lblSolicitudIdText = new LabelField( "Nro. de Sol.", FIELD_LEFT | FIELD_VCENTER );
        lblSolicitudIdValue = new LabelField(""+sol_compra.getSolicitudCompraId(), FIELD_VCENTER | FIELD_HCENTER);

        TwoColumnField both_sol_id = new TwoColumnField( lblSolicitudIdText, 
        													lblSolicitudIdValue, USE_ALL_WIDTH );
        both_sol_id.setMargin( 6, 0, 6, 0 ); 
        
        lblFechaSolicitudText = new LabelField( "Fecha", FIELD_LEFT | FIELD_VCENTER );
        lblFechaSolicitudValue = new LabelField(Tool.getInstance().parseDateToString(sol_compra.getFechaSolicitudCompra(), 6), FIELD_VCENTER | FIELD_HCENTER);
        
        TwoColumnField both_fecha_sol = new TwoColumnField( lblFechaSolicitudText, 
        														lblFechaSolicitudValue, USE_ALL_WIDTH );
        both_fecha_sol.setMargin( 6, 0, 6, 0 );
        
        lblMontoText = new LabelField( "Monto(BsF.)", FIELD_LEFT | FIELD_VCENTER );
        lblMontoValue = new LabelField(""+sol_compra.getTotalSolicitud(), FIELD_VCENTER | FIELD_HCENTER);
        
        TwoColumnField both_monto_sol = new TwoColumnField( lblMontoText, 
        		lblMontoValue, USE_ALL_WIDTH );
        both_monto_sol.setMargin( 6, 0, 6, 0 );
        
        tcfm_solicitud.add(both_sol_id);
        tcfm_solicitud.add(both_fecha_sol);
        tcfm_solicitud.add(both_monto_sol);
		
	}
	
	public void pintarListaArticuloCarrito()
	{
		showListField();
	}
	
	public void pintarVacio()
	{
		//tcfm_pantalla.deleteAll();
		buttonSet.deleteAll();
		lblVacio = new LabelField("<< VACIO >>", USE_ALL_WIDTH | 
				DrawStyle.HCENTER |
				DrawStyle.VCENTER );
		buttonSet.add(lblVacio);
	}
	
	public void showListField()
	{
		buttonSet.deleteAll();
		ArticuloCarritoListButtonField link;
		ListaArticuloCarrito listado = sol_compra.getListadoArticulo();
		for(int i = 0; i < listado.cantElementListaArticulo() ; i++) 
		{
			
			link = new ArticuloCarritoListButtonField(listado.getArticuloCarritoAtIndex(i), LabelField.ELLIPSIS) 
			{
			
				protected void paintBackground( Graphics g )
				{
					  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
					  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

				      if( _drawPosition < 0 ) 
				      {
				          // it's like a list field, let the default background be drawn
				          super.paintBackground( g );
				          return;
				      }
				      
				      int oldColour = g.getColor();
				      
				      int focus = COLOR_BACKGROUND_FOCUS;
				      if(_doAction) 
				    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
				      
				      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_BACKGROUND;
				      try 
				      {
				          if( _drawPosition == 0 ) 
				          {
				              // Top
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else if( _drawPosition == 1 ) 
				          {
				              // Bottom 
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				          } 
				          else if( _drawPosition == 2 ) 
				          {
				              // Middle
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else 
				          {
				              // Single
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				          }
				      } 
				      finally 
				      {
				          g.setColor( oldColour );
				      }
				}
			};

			link.setChangeListener( this );
	        buttonSet.add( link );
		}

	}
	
	
	public void fieldChanged( Field field, int context ) 
	{
		//ScreenEngine.getInstance().goDetailsArticuloCarrito(lista_articulo.getArticuloCarritoAtIndex(field.getIndex()));
		
	}	
}


